<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户表</title>
    <link rel="stylesheet" href="http://47.100.102.177:8080/app/css/common/bootstrap.min.css">
    <link rel="stylesheet" href="http://47.100.102.177:8080/app/plugin/download/font_2801350_hw9pyy8c125/iconfont.css">    <script src="http://47.100.102.177:8080/app/js/common/jquery-1.12.4.js"></script>
    <!-- 必须先引入vue，  后使用element-ui -->
    <script src="../../../boot/plugin/element/vue.js"></script>
    <!--引入 element-ui 的样式，-->
    <link rel="stylesheet" href="../../../boot/plugin/element/element.css">
    <!-- 引入element 的组件库-->
    <script src="../../../boot/plugin/element/element.js"></script>
    <style>
        th {
            text-align: center;
        }

        #back {
            width: 100%;
            height: 100vh;
            position: absolute;
            top: 0px;
            background-color: rgba(0, 0, 0, 0.2);
            display: none;
        }

        .inf {
            background-color: white;
            height: 630px;
            width: 45%;
            margin: auto;
            margin-top: 60px;
            border-radius: 3px;
        }

        .inf-head {
            width: 100%;
            height: 30px;
            background-color: rgb(21, 184, 215);
            border-radius: 3px;
        }

        #close-add {
            float: right;
            margin-right: 5px;
            background-color: red;
            color: white;
            font-weight: 900;
            border-radius: 5px;
            border: 0px;
            margin-top: 3px;
        }

        .inf-from {
            width: 100%;
            height: 600px;
            border-radius: 3px;
            overflow: scroll;
        }

        .mailTable {
            width: 100%;
            border-top: 1px solid #E6EAEE;
            border-left: 1px solid #E6EAEE;
            margin-bottom: 30px;
        }

        .mailTable tr td,
        .mailTable1 tr td {
            width: 200px;
            height: 35px;
            line-height: 35px;
            box-sizing: border-box;
            padding: 10px;
            border-bottom: 1px solid #E6EAEE;
            border-right: 1px solid #E6EAEE;
        }

        .mailTable tr td.column {
            background-color: #EFF3F6;
            color: #393C3E;
            width: 30%;
        }
    </style>
</head>

<body>
    <div style="width: 100%;height: 20px;"></div>
    <form class="form-inline" style="text-align: center;" id="user_form">
        <div class="form-group">
            <label for="account">账号</label>
            <input type="text" class="form-control" name="phone" id="account" placeholder="请输入账号">
            <label for="nickName">昵称</label>
            <input type="text" class="form-control" name="name" id="nickName" placeholder="请输入昵称">
        </div>
        <button type="button" class="btn btn-default" onclick="searchUser()"><i class="iconfont"
                style="font-size: 14px;color: #3E8EF7;">&#xe660;</i></button>
    </form>
    <div style="width: 100%;height: 20px;"></div>
    <table class="table table-bordered" style="text-align: center;">
        <thead id="th">
            <tr>
                <th>用户人员id</th>
                <th>昵称(用户名)</th>
                <th>账号</th>
                <th>收货地址</th>
                <th>状态(双击修改)</th>
            </tr>
        </thead>
        <tbody id="tb">

        </tbody>


    </table>
    <div style="float: right;margin-right: 20px;">
        <div style="float: left;height: 80px;line-height: 80px;margin-right: 20px;" id="pageNum">共&nbsp;<span style="color: red">${haveUser}</span>&nbsp;条记录&nbsp;&nbsp;</div>
        <div style="float: left;height: 80px;line-height: 80px;margin-right: 20px;">&nbsp;<span style="color: red">12</span>&nbsp;条&nbsp;/&nbsp;页</div>
        <div id="app" style="float: left;" onclick="page()">
            <el-pagination background layout="prev, pager, next" :total="${allPageUser}*10" style="margin-top: 23px;">
            </el-pagination>
        </div>
    </div>
    <div id="back" style="position: fixed;">
        <div class="inf">
            <div class="inf-head">
                <h4 style="color: white;display: inline;line-height: 30px;margin-left: 5px;">地址查看</h4>
                <input type="button" id="close-add" value="×">
            </div>
            <div class="inf-from">
                <table class="mailTable" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="column">用户人员id</td>
                        <td id="peopleId"></td>
                    </tr>
                    <tr>
                        <td class="column">昵称(用户名)</td>
                        <td id="peopleName"></td>
                    </tr>
                    <tr>
                        <td class="column">账号(手机号)</td>
                        <td id="peoplePhone"></td>
                    </tr>
                    <tr>
                        <td class="column">地址</td>
                        <td id="peopleAddress"></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        new Vue().$mount('#app');

        let user_list = ${pageUser};
        let tb = document.getElementById("tb");
        let str = "";
        for (let i = 0; i < user_list.length; i++) {
            if (user_list[i].status=="账户正常"){
                str += '<tr style="background:#5CB85C;color: white ">';
            }else if (user_list[i].status=="账户冻结"){
                str += '<tr style="background:#31B0D5;color: white ">';
            }else {
                str += '<tr style="background:#D43F3A;color: white ">';
            }
            str += '<td>' + user_list[i].peopleId + '</td>';
            str += '<td>' + user_list[i].nickName + '</td>';
            str += '<td>' + user_list[i].account + '</td>';
            str += '<td><button class="btn-link" id='+user_list[i].peopleId+' type="button" onclick="findAddress(this)" style="color: white;">查看地址</button></td>';
            str += '<td>' + user_list[i].status + '</td>';
            str += '</tr>';
        }
        tb.innerHTML = str;

        let len = document.getElementById("tb").getElementsByTagName("tr").length;
        let trs = document.getElementById("tb").getElementsByTagName("tr");
        for (let i = 0; i < len; i++) {
            let td = trs[i].querySelector('td:last-child');
            if (td.innerText!="账户封号"){
                td.ondblclick = update;
            }
        }

        function page() {
            str="";
            let active = document.getElementsByClassName("active")[0];

            let param = "nowPage="+parseInt(active.innerText);
            $.get('/boot/admin/getNowUser',param,function (res){
                user_list = res;
                for (let i = 0; i < user_list.length; i++) {
                    if (user_list[i].status=="账户正常"){
                        str += '<tr style="background:#5CB85C;color: white ">';
                    }else if (user_list[i].status=="账户冻结"){
                        str += '<tr style="background:#31B0D5;color: white ">';
                    }else {
                        str += '<tr style="background:#D43F3A;color: white ">';
                    }
                    str += '<td>' + user_list[i].peopleId + '</td>';
                    str += '<td>' + user_list[i].nickName + '</td>';
                    str += '<td>' + user_list[i].account + '</td>';
                    str += '<td><button class="btn-link" id='+user_list[i].peopleId+' type="button" onclick="findAddress(this)" style="color: white;">查看地址</button></td>';
                    str += '<td>' + user_list[i].status + '</td>';
                    str += '</tr>';
                }
                tb.innerHTML = str;

                let len = document.getElementById("tb").getElementsByTagName("tr").length;
                let trs = document.getElementById("tb").getElementsByTagName("tr");
                for (let i = 0; i < len; i++) {
                    let td = trs[i].querySelector('td:last-child');
                    if (td.innerText!="账户封号"){
                        td.ondblclick = update;
                    }
                }
            });
        }

        function searchUser(){
            let form = $("#user_form");
            let param = form.serialize();
            str = "";
            $.post('/boot/admin/findUser',param,function(res){
                if (res==null){
                    str +='<tr><td colspan="7">查询为空</td></tr>';
                    tb.innerHTML = str;
                }else if(res[0]==null){
                    str +='<tr><td colspan="7">查询为空</td></tr>';
                    tb.innerHTML = str;
                }else {
                    user_list = res;
                    for (let i = 0; i < user_list.length; i++) {
                        if (user_list[i].status=="账户正常"){
                            str += '<tr style="background:#5CB85C;color: white ">';
                        }else if (user_list[i].status=="账户冻结"){
                            str += '<tr style="background:#31B0D5;color: white ">';
                        }else {
                            str += '<tr style="background:#D43F3A;color: white ">';
                        }
                        str += '<td>' + user_list[i].peopleId + '</td>';
                        str += '<td>' + user_list[i].nickName + '</td>';
                        str += '<td>' + user_list[i].account + '</td>';
                        str += '<td><button class="btn-link" id='+user_list[i].peopleId+' type="button" onclick="findAddress(this)" style="color: white;">查看地址</button></td>';
                        str += '<td>' + user_list[i].status + '</td>';
                        str += '</tr>';
                    }
                    tb.innerHTML = str;

                    let len = document.getElementById("tb").getElementsByTagName("tr").length;
                    let trs = document.getElementById("tb").getElementsByTagName("tr");
                    for (let i = 0; i < len; i++) {
                        let td = trs[i].querySelector('td:last-child');
                        if (td.innerText!="账户封号"){
                            td.ondblclick = update;
                        }
                    }
                }
            })
        }

        function update() {
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                let oldValue = this.innerText;
                this.innerHTML = "";
                let select = document.createElement("select");
                let td = this;
                let tr = td.parentNode;
                select.style.backgroundColor = tr.style.backgroundColor;
                select.innerHTML = "<option value='1'>账户正常</option><option value='2'>账户冻结</option><option value='3'>账户封号</option>";
                select.onblur = function() {
                    let statusId = parseInt(this.options[this.selectedIndex].value);
                    let peopleId = parseInt(tr.firstChild.innerText);
                    let param = {statusId:statusId,peopleId:peopleId}
                    if (this.options[this.selectedIndex].text=="账户封号"){
                        if(confirm("一旦封号就不可回退,你确定执行此操作吗?")){
                            $.get('/boot/admin/updateUser',param,function (res){
                                if (res.code=="200"){
                                    alert(res.msg);
                                    location.reload();
                                }else {
                                    alert("修改失败");
                                }
                            })
                        }
                    }else {
                        if(confirm("你确定执行此操作吗?")) {
                            $.get('/boot/admin/updateUser', param, function (res) {
                                if (res.code=="200"){
                                    alert(res.msg);
                                    location.reload();
                                }else {
                                    alert("修改失败");
                                }
                            })
                        }
                    }
                }
                td.appendChild(select);
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }
        }
        let tbody = document.getElementById("tb");

        function findAddress(obj){
            let param = "peopleId="+parseInt(obj.id);
            $.get('/boot/admin/getPeopleAddress',param,function (res){
                if (res!=null&&res[0]!=null){
                    $("#back").css("display", "block");
                    $("#peopleId")[0].innerHTML=obj.id;
                    $("#peopleName")[0].innerHTML=res[0].name;
                    $("#peoplePhone")[0].innerHTML=res[0].phone;
                    let addressStr = "";
                    for (let i=0;i<res.length;i++){
                        addressStr += '('+(parseInt(i)+1)+')';
                        addressStr += res[i].address;
                        addressStr +='<br>';
                    }
                    $("#peopleAddress")[0].innerHTML=addressStr;
                }else {
                    alert("sorry!该用户没有新增地址!!");
                }
            });
        }



        $(function() {
            $("#account").bind("input propertychange", function() {
                var text = $("#account").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#nickName").attr("disabled", "disabled"); //设置输入框2不可用
                } else {
                    $("#nickName").removeAttr("disabled"); //移除不可用的属性
                }
            });
            $("#nickName").bind("input propertychange", function() {
                var text = $("#nickName").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#account").attr("disabled", "disabled"); //设置输入框2不可用
                } else {
                    $("#account").removeAttr("disabled"); //移除不可用的属性
                }
            });

            $("#close-add").click(function (){
                $("#back").css("display", "none");
            })

        });
    </script>
</body>

</html>